package com.example.javabase.ScriptEngineManager;

import com.example.javabase.DesignPattern.Strategy.ScriptApiCalculateHandler;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: wyb
 * @date: 2021/11/3 10:27
 * @describe:
 * @vision 1.0
 */
public class Test1 {

    public Double Calculation(String formula, Map<String,Object> instrsValue, List<String> replaceInstrs){
        String excueFormula = null;

        replaceInstrs.forEach(instr->{
//            excueFormula = replaceStr(formula, instr, instrsValue.get(instr).toString());
        });
        Double result=null;  //计算结果
        ScriptEngineManager manager = new ScriptEngineManager();   //创建一个ScriptEngineManager对象
        ScriptEngine engine = manager.getEngineByName("js");  //通过ScriptEngineManager获得ScriptEngine对象
        try {
                result = (Double) engine.eval(formula);  //用ScriptEngine的eval方法执行脚本
        } catch (ScriptException e) {
//            this.isSuccess=false;
            result=Double.NaN;
        }
        return result;
    }

    /**
     * @Author wyb
     * @Description  精确替换字符 防止出现 匹配A 时将AA匹配的情况
     * @Date 10:29 2021/11/3
     * @Param [sourceStr:替换公式字的符串
     *         replaceKey：公式中要替换的字母
     *         replaceValue:要替换成的数值]
     * @return java.lang.String
     **/
    public static String replaceStr(String sourceStr,String replaceKey,String replaceValue){
        String replaceStrReg="";
        for(char str_char:replaceKey.toCharArray()){
            replaceStrReg+="[";
            replaceStrReg+=str_char;
            replaceStrReg+="]";
        }
        String startReg="^"+replaceStrReg+"([\\+\\-\\*/,)])";
        String endReg="([\\+\\-\\*/,(])"+replaceStrReg+"$";
        String reg="([^a-zA-Z])("+replaceStrReg+")"+"([^a-zA-Z])";
        String endStr=sourceStr;
        while(matcheStr(endStr,replaceKey)){
                endStr=endStr.replaceAll(startReg, replaceValue+"$1");
            endStr=endStr.replaceAll(reg, "$1"+replaceValue+"$3");
            endStr=endStr.replaceAll(endReg, "$1"+replaceValue);
        }
        return endStr;
    }

    public static Boolean matcheStr(String sourceStr,String matchStr){
        String replaceStrReg="";
        for(char str_char:matchStr.toCharArray()){
            replaceStrReg+="[";
            replaceStrReg+=str_char;
            replaceStrReg+="]";
        }
        String startReg="^"+replaceStrReg+"([\\+\\-\\*/,)])[\\s\\S]*";
        String endReg="[\\s\\S]*([\\+\\-\\*/,(])"+replaceStrReg+"$";
        String reg="[\\s\\S]*([^A-Za-z])("+replaceStrReg+")"+"([^A-Za-z])[\\s\\S]*";
        if(sourceStr.matches(startReg)||sourceStr.matches(reg)||sourceStr.matches(endReg)){
            return true;
        }else{
            return false;
        }
    }

    public static void main(String[] args) {
        String sourceStr="aaaa-aa+aaa";
        String replaceKey="aa";
        String replaceValue="0.1";
        String s = replaceStr(sourceStr, replaceKey, replaceValue);
        System.out.println(s);

    }





}
